package com.seven.monior.AppInfo;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;

public class AppInfo implements Serializable {
	private String m_packageName = "";
	private String m_appName = "";
	private String m_pName = "";
	private String m_versionName = "";
	private int m_versionCode = 0;
	private Drawable m_icon;
	private ArrayList<String> m_permissions;
	private ArrayList<RunningAppProcessInfo> m_runningProcesses;
	private Long m_lRx = 0L;
	private Long m_lTx = 0L;

	private Boolean m_bIsRunning = false;

	public AppInfo() {

	}

	public AppInfo(String packageName, String appName, String pName,
			String versionName, int versionCode, Drawable icon,
			ArrayList<String> permissions, Boolean isRunning,
			ArrayList<RunningAppProcessInfo> processes) {
		// TODO Auto-generated constructor stub
		super();
		m_packageName = packageName;
		m_appName = appName;
		m_pName = pName;
		m_versionName = versionName;
		m_versionCode = versionCode;
		m_icon = icon;
		m_permissions = permissions;
		// m_bIsRunning=isRunning;
		m_runningProcesses = processes;
	}

	public String getPackageName() {
		return m_packageName;
	}

	public String getAppName() {
		return m_appName;
	}

	public String getPName() {
		return m_pName;
	}

	public String getVersionName() {
		return m_versionName;
	}

	public int getVersionCode() {
		return m_versionCode;
	}

	public Drawable getIcon() {
		return m_icon;
	}

	public ArrayList<String> getPermissions() {
		return m_permissions;
	}

	public Boolean getIsRunning() {
		return m_bIsRunning;
	}

	public String AppInfoDetail() {
		String str = m_appName + "\t" + m_pName + "\t" + m_versionName + "\t"
				+ m_versionCode + "\t";
		return str;
	}

	public boolean addProcessInfo(RunningAppProcessInfo processPkg) {
		m_runningProcesses.add(processPkg);
		m_bIsRunning = true;
		return true;
	}

	public ArrayList<RunningAppProcessInfo> getRunningAppProcessInfos() {
		return m_runningProcesses;
	}

	public Long getRxBytes() {
		return m_lRx;
	}

	public Long getTxBytes() {
		return m_lTx;
	}

	public void prettyPrint() {
		// Log(appName + "\t" + pName + "\t" + versionName + "\t" + versionCode
		// + "\t");
	}

	private static final String SCHEME = "package";
	/**
	 * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.1及之前版本)
	 */
	private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
	/**
	 * 调用系统InstalledAppDetails界面所需的Extra名称(用于Android 2.2)
	 */
	private static final String APP_PKG_NAME_22 = "pkg";
	/**
	 * InstalledAppDetails所在包名
	 */
	private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
	/**
	 * InstalledAppDetails类名
	 */
	private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";

	/**
	 * 调用系统InstalledAppDetails界面显示已安装应用程序的详细信息。 对于Android 2.3（Api Level
	 * 9）以上，使用SDK提供的接口； 2.3以下，使用非公开的接口（查看InstalledAppDetails源码）。
	 * 
	 * @param context
	 * 
	 * @param packageName
	 *            应用程序的包名
	 */
	public static void showInstalledAppDetails(Context context,
			String packageName) {
		Intent intent = new Intent();
		final int apiLevel = Build.VERSION.SDK_INT;
		if (apiLevel >= 9) { // 2.3（ApiLevel 9）以上，使用SDK提供的接口
		// intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
		// Uri uri = Uri.fromParts(SCHEME, packageName, null);
		// intent.setData(uri);
		} else { // 2.3以下，使用非公开的接口（查看InstalledAppDetails源码）
			// 2.2和2.1中，InstalledAppDetails使用的APP_PKG_NAME不同。
			final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
					: APP_PKG_NAME_21);
			intent.setAction(Intent.ACTION_VIEW);
			intent.setClassName(APP_DETAILS_PACKAGE_NAME,
					APP_DETAILS_CLASS_NAME);
			intent.putExtra(appPkgName, packageName);
		}
		context.startActivity(intent);
	}

}
